package com.cet.qz.unit01.chapter1_1;

import edu.princeton.cs.algs4.StdDraw;
import edu.princeton.cs.algs4.StdRandom;

import java.awt.*;

/**
 * @program: algorithm
 * @description:
 * @author: 陈恩涛
 * @create: 2020-12-19 21:44
 **/
public class Qz1_1_31MY {

    public static void drawCircle(double x0,double y0,double radius){
        //画布区域,第一层要设置好画布，后面都在这一张画布上作图了
        StdDraw.setXscale(x0 - radius,x0 + radius);
        StdDraw.setYscale(y0 - radius,y0 + radius);

        StdDraw.setPenRadius(0.004);
        StdDraw.setPenColor(Color.blue);

        StdDraw.circle(x0,y0,radius);
    }

    public static double[][] drawPoints(double x0,double y0,double radius,int N){

        StdDraw.setPenRadius(0.01);
        StdDraw.setPenColor(Color.red);
        double[][] points = new double[N][2];
        for (int i = 0; i < N; i++) {
            double x = x0 + radius*Math.cos(2*i*Math.PI/N);
            double y = y0 + radius*Math.sin(2*i*Math.PI/N);
            //System.out.println("x :"+x+", y:"+y);

            StdDraw.point(x, y);
            points[i][0] = x;
            points[i][1] = y;
        }
        return points;

    }

    //N个点共有N*(N-1)/2条线段
    public static double[][] getAllLines(double[][] points){
        int N = points.length;
        int n = N*(N-1)/2;
        double[][] lines = new double[n][4];

        //用于判断两个点是否已经组成线段
        boolean[][] flags = new boolean[N][N];
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++) {
                if(i == j)
                    flags[i][j] = true;
            }
        }
        int k = 0;

        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++) {
                if (!flags[i][j] && !flags[j][i]) {
                    lines[k][0] = points[i][0];
                    lines[k][1] = points[i][1];
                    lines[k][2] = points[j][0];
                    lines[k][3] = points[j][1];

                    flags[i][j] = true;
                    k++;
                }
            }
        }

        return lines;
    }

    public static void randomLinkPoints(double[][] lines,double probablility){

        StdDraw.setPenRadius(0.005);
        StdDraw.setPenColor(Color.GREEN);

        int N = lines.length;
        for (int i = 0; i < N; i++) {
            if (StdRandom.bernoulli(probablility)) {
                StdDraw.line(lines[i][0],lines[i][1],lines[i][2],lines[i][3]);
            }
        }

    }

    public static void main(String[] args) {
        double x0 = 2.0;
        double y0 = 3.0;
        double radius = 4.0;
        drawCircle(x0,y0,radius);
        double[][] points = drawPoints(x0, y0, radius, 20);
        double[][] lines = getAllLines(points);
        randomLinkPoints(lines,1);
    }
}
